package org.springside.modules.persistence;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.Map;

public class SearchDataTransform {
	public enum SearchDataType {
		String, Date, Boolean
	}

	private String fieldName;
	private SearchDataType dataType;
	private String attachMent;

	public SearchDataTransform(String fieldName, SearchDataType dataType, String attachMent) {
		super();
		this.fieldName = fieldName;
		this.dataType = dataType;
		this.attachMent = attachMent;
	}

	public String getFieldName() {
		return fieldName;
	}

	public void setFieldName(String fieldName) {
		this.fieldName = fieldName;
	}

	public SearchDataType getDataType() {
		return dataType;
	}

	public void setDataType(SearchDataType dataType) {
		this.dataType = dataType;
	}

	public String getAttachMent() {
		return attachMent;
	}

	public void setAttachMent(String attachMent) {
		this.attachMent = attachMent;
	}

	public static void dateTypeTransform(Map<String, SearchFilter> filters, Map<String, SearchDataTransform> dataTyps) {

		dataTyps.forEach((key, dataTransform) -> {
			SearchFilter filter = filters.get(key);
			if (filter != null) {
				switch (dataTransform.getDataType()) {
				case Date:
					try {
						filter.value = (new SimpleDateFormat(dataTransform.attachMent)).parse(filter.value.toString());
					} catch (ParseException e) {
						e.printStackTrace();
					}
					break;
				case Boolean:
					if(filter.value instanceof String){
						filter.value = "true".equalsIgnoreCase(filter.value.toString());
					}					
					break;
				default:
					break;
				}
			}
		});

	}
}
